﻿using System;
using UnityEngine;

// Token: 0x02000154 RID: 340
[AddComponentMenu("RVP/C#/Vehicle Controllers/Vehicle Balance", 4)]
[DisallowMultipleComponent]
[RequireComponent(typeof(VehicleParent))]
public class VehicleBalance : MonoBehaviour
{
	// Token: 0x0600061D RID: 1565 RVA: 0x00032A27 File Offset: 0x00030C27
	private void Start()
	{
		this.tr = base.transform;
		this.rb = base.GetComponent<Rigidbody>();
		this.vp = base.GetComponent<VehicleParent>();
	}

	// Token: 0x0600061E RID: 1566 RVA: 0x00032A50 File Offset: 0x00030C50
	private void FixedUpdate()
	{
		this.actualPitchInput = ((this.vp.wheels.Length != 1) ? Mathf.Clamp(this.vp.pitchInput, -1f, (float)((this.vp.velMag <= this.endoSpeedThreshold) ? 1 : 0)) : 0f);
		if (this.vp.groundedWheels > 0 && this.leanFactor != Vector3.zero)
		{
			this.ApplyLean();
		}
	}

	// Token: 0x0600061F RID: 1567 RVA: 0x00032AE0 File Offset: 0x00030CE0
	private void ApplyLean()
	{
		if (this.vp.groundedWheels > 0)
		{
			Vector3 direction = this.vp.norm.InverseTransformDirection((Vector3.Dot(this.vp.wheelNormalAverage, GlobalControl.worldUpDir) > 0f) ? Vector3.Lerp(GlobalControl.worldUpDir, this.vp.wheelNormalAverage, Mathf.Abs(Vector3.Dot(this.vp.norm.up, GlobalControl.worldUpDir)) * 2f) : this.vp.wheelNormalAverage);
			Debug.DrawRay(this.tr.position, this.vp.norm.TransformDirection(direction), Color.white);
			this.targetLean = new Vector3(Mathf.Lerp(direction.x, Mathf.Clamp(-this.vp.rollInput * this.leanFactor.z * this.leanRollCurve.Evaluate(Mathf.Abs(this.vp.localVelocity.z)) + Mathf.Clamp(this.vp.localVelocity.x * this.slideLeanFactor, -this.leanFactor.z * this.slideLeanFactor, this.leanFactor.z * this.slideLeanFactor), -this.leanFactor.z, this.leanFactor.z), Mathf.Max(new float[]
			{
				Mathf.Abs(F.MaxAbs(new float[]
				{
					this.vp.steerInput,
					this.vp.rollInput
				}))
			})), Mathf.Pow(Mathf.Abs(this.actualPitchInput), this.pitchExponent) * Mathf.Sign(this.actualPitchInput) * this.leanFactor.x, direction.z * (1f - Mathf.Abs(F.MaxAbs(new float[]
			{
				this.actualPitchInput * this.leanFactor.x,
				this.vp.rollInput * this.leanFactor.z
			}))));
		}
		else
		{
			this.targetLean = this.vp.upDir;
		}
		this.targetLeanActual = Vector3.Lerp(this.targetLeanActual, this.vp.norm.TransformDirection(this.targetLean), (1f - this.leanSmoothness) * Time.timeScale * TimeMaster.inverseFixedTimeFactor).normalized;
		Debug.DrawRay(this.tr.position, this.targetLeanActual, Color.black);
		this.rb.AddTorque(this.vp.norm.right * -(Vector3.Dot(this.vp.forwardDir, this.targetLeanActual) * 20f - this.vp.localAngularVel.x) * 100f * ((this.vp.wheels.Length != 1) ? this.leanPitchCurve.Evaluate(Mathf.Abs(this.actualPitchInput)) : 1f), ForceMode.Acceleration);
		this.rb.AddTorque(this.vp.norm.forward * ((this.vp.groundedWheels != 1) ? 0f : (this.vp.steerInput * this.leanFactor.y - this.vp.norm.InverseTransformDirection(this.rb.angularVelocity).z)) * 100f * this.leanYawCurve.Evaluate(Mathf.Abs(this.vp.steerInput)), ForceMode.Acceleration);
		this.rb.AddTorque(this.vp.norm.up * (-Vector3.Dot(this.vp.rightDir, this.targetLeanActual) * 20f - this.vp.localAngularVel.z) * 100f, ForceMode.Acceleration);
		if (this.vp.groundedWheels == 1 && this.leanFactor.y > 0f)
		{
			this.rb.AddTorque(this.vp.norm.TransformDirection(new Vector3(0f, 0f, this.vp.steerInput * this.leanFactor.y - this.vp.norm.InverseTransformDirection(this.rb.angularVelocity).z)), ForceMode.Acceleration);
		}
	}

	// Token: 0x0400098E RID: 2446
	private Transform tr;

	// Token: 0x0400098F RID: 2447
	private Rigidbody rb;

	// Token: 0x04000990 RID: 2448
	private VehicleParent vp;

	// Token: 0x04000991 RID: 2449
	private float actualPitchInput;

	// Token: 0x04000992 RID: 2450
	private Vector3 targetLean;

	// Token: 0x04000993 RID: 2451
	private Vector3 targetLeanActual;

	// Token: 0x04000994 RID: 2452
	[Tooltip("Lean strength along each axis")]
	public Vector3 leanFactor;

	// Token: 0x04000995 RID: 2453
	[Range(0f, 0.99f)]
	public float leanSmoothness;

	// Token: 0x04000996 RID: 2454
	[Tooltip("Adjusts the roll based on the speed, x-axis = speed, y-axis = roll amount")]
	public AnimationCurve leanRollCurve = AnimationCurve.Linear(0f, 0f, 10f, 1f);

	// Token: 0x04000997 RID: 2455
	[Tooltip("Adjusts the pitch based on the speed, x-axis = speed, y-axis = pitch amount")]
	public AnimationCurve leanPitchCurve = AnimationCurve.Linear(0f, 0f, 1f, 1f);

	// Token: 0x04000998 RID: 2456
	[Tooltip("Adjusts the yaw based on the speed, x-axis = speed, y-axis = yaw amount")]
	public AnimationCurve leanYawCurve = AnimationCurve.Linear(0f, 0f, 1f, 1f);

	// Token: 0x04000999 RID: 2457
	[Tooltip("Speed above which endos (forward wheelies) aren't allowed")]
	public float endoSpeedThreshold;

	// Token: 0x0400099A RID: 2458
	[Tooltip("Exponent for pitch input")]
	public float pitchExponent;

	// Token: 0x0400099B RID: 2459
	[Tooltip("How much to lean when sliding sideways")]
	public float slideLeanFactor = 1f;
}
